<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;

class PieceRateDetailMonth extends BaseController
{
    protected $table='jijianmingxi_view';
    protected $title='计件工资月汇总';
    /**
     * @autho:xiangzi
     * @auth:true
     * @Date:2024/7/10
     *
     */
    public function index()
    {

        $param=$this->param;
        if (request()->isAjax()) {
            $where[]=['status','=',2];
            if (isset($param['orderDate'])&&$param['orderDate']){
                $bt=date('Y-m-01',strtotime($this->param['orderDate']));
                $et=date('Y-m-t',strtotime($this->param['orderDate']));
                $where[]=["orderDate",'between',[$bt,$et]];
            }
            foreach (['craftName','teamName','userName','bar_code','mdName','specs'] as $v){
                if (isset($param[$v])&&$param[$v]){
                    $where[]=[$v,'like','%'.$param[$v].'%'];
                }
            }
            foreach (['report_id','status','pricing_mode'] as $v){
                if (isset($param[$v])&&$param[$v]){
                    $where[]=[$v,'=',$param[$v]];
                }
            }

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->where($where)
                ->order('id DESC')
                ->field("*,DATE_FORMAT(orderDate, '%Y-%m') as orderDate")
                ->group("DATE_FORMAT(orderDate, '%Y-%m'),uid")
                ->paginate($rows)->each(function ($item){
                    $item['sumSalary']=Db::name($this->table)
                        ->where('status',2)
                        ->whereMonth('orderDate',$item['orderDate'])
                        ->where('uid',$item['uid'])
                        ->sum('salary');
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            return view();
        }

    }
}